package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.CommonUtil;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.SharedPreferencesUtil;
import com.jucai.pppeiyouclient.utils.ToastUtils;
import com.jucai.pppeiyouclient.weidget.GetVerTextView;

import java.util.HashMap;

public class PassChangeActivity extends BaseActivity {
    private TextView changePhone, changeSubmit;
    private EditText changeVerification, changeNewPass;
    private GetVerTextView changeGetVerification;


    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_pass_change);

        ((TextView) findViewById(R.id.common_title_text)).setText("修改密码");
        changePhone = (TextView) findViewById(R.id.change_phone);
        changeGetVerification = (GetVerTextView) findViewById(R.id.change_get_verification);
        changeSubmit = (TextView) findViewById(R.id.change_submit);
        changeVerification = (EditText) findViewById(R.id.change_verification);
        changeNewPass = (EditText) findViewById(R.id.change_new_pass);
    }

    @Override
    protected void initData() {
        changePhone.setText(SharedPreferencesUtil.getStringPreference(PassChangeActivity.this, "PREFERENCE", "phone_number", ""));
        changeVerification.setSingleLine();
    }

    @Override
    protected void loadNetData() {

    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        changeGetVerification.setOnClickListener(this);
        changeSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;
            case R.id.common_home_back:
                startActivity(new Intent(PassChangeActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;

            case R.id.change_get_verification:
                if (changeGetVerification.getIsHandler()) {
                    return;
                } else {
                    changeGetVerification.setHandler();
                    HashMap<String, String> param = new HashMap<>();
                    param.put("service", "Public_VerifyCode.SendResetPwCode");
                    param.put("member_phone", changePhone.getText().toString());

                    CommonRequestNetData.getJsonStringData(PassChangeActivity.this, param, true,new CommonRequestNetData.JsonStringCallBack() {
                        @Override
                        public void parseJsonString(String str) {
                            LogUtils.e("get ver --> str = " + str);
                            LogUtils.e(str);
                        }
                    });
                }
                break;

            case R.id.change_submit:
                CommonUtil.hideSoftInput(changeNewPass);
                if (changeNewPass.getText().toString().length() < 8) {
                    ToastUtils.showToast("密码长度需要大于8");
                } else {
                    HashMap<String, String> changeParam = new HashMap<>();
                    changeParam.put("password", changeNewPass.getText().toString());
                    changeParam.put("member_phone", changePhone.getText().toString());
                    changeParam.put("verifycode", changeVerification.getText().toString());
                    changeParam.put("service", "Client_UpdatePassword.go");

                    CommonRequestNetData.getJsonStringData(PassChangeActivity.this, changeParam,true, new CommonRequestNetData.JsonStringCallBack() {
                        @Override
                        public void parseJsonString(String str) {
                            LogUtils.e("change ---> str = " + str);
                            ToastUtils.showToast(str);
                            ActivityCollector.removeActivity(PassChangeActivity.this);
                        }
                    });
                }
                break;
        }
    }
}
